package com.lgq.cfw.service.impl;

import com.lgq.cfw.bean.TableRolePermission;
import com.lgq.cfw.bean.example.TableRolePermissionExample;
import com.lgq.cfw.bean.example.TableRolePermissionExample.Criteria;
import com.lgq.cfw.dao.TableRolePermissionMapper;
import com.lgq.cfw.service.RoleAndPermissionService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Lin
 * @date 2019-02-10 18:56
 **/
@Service
public class RoleAndPermissionServiceImpl implements RoleAndPermissionService {

  @Autowired
  TableRolePermissionMapper tableRolePermissionMapper;

  /**
   * 更新角色的权限信息
   * @param roleId 角色ID
   * @param permissionIdString 前台传来的被选中的权限ID字符串
   */
  @Override
  public void update(Integer roleId, String permissionIdString) {
    //先删除该角色的所有权限
    TableRolePermissionExample tableRolePermissionExample=new TableRolePermissionExample();
    Criteria criteria= tableRolePermissionExample.createCriteria();
    criteria.andRoleidEqualTo(roleId);
    tableRolePermissionMapper.deleteByExample(tableRolePermissionExample);
    //重新添加角色权限
    String[] permissionIdStringArray = permissionIdString.split("-");
    for (String permissionId : permissionIdStringArray) {
      TableRolePermission tableRolePermission=new TableRolePermission();
      tableRolePermission.setRoleid(roleId);
      tableRolePermission.setPermissionid(Integer.parseInt(permissionId));
      tableRolePermissionMapper.insertSelective(tableRolePermission);
    }
  }
}
